package wiz.client.ui.viewimpl;

import wiz.client.WizStrings;
import wiz.client.activities.selectplayers.SelectPlayerTableModel;
import wiz.client.activities.selectplayers.SelectPlayersView;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

public class SelectPlayersViewImpl extends Composite implements SelectPlayersView {

	private static SelectPlayersViewImplUiBinder uiBinder = GWT.create(SelectPlayersViewImplUiBinder.class);

	private final WizStrings fWizStrings = GWT.create(WizStrings.class);

	
	interface SelectPlayersViewImplUiBinder extends UiBinder<Widget, SelectPlayersViewImpl> {
	}

	

	@UiField
	Button fDoneButton;
	
	@UiField
	HasText fPlayerNameText;
	
	@UiField
	Button fAddPlayerButton;
	
	
	
	
	@UiField
	CellTable<SelectPlayerTableModel> fPlayerSelectionTable;

	private Column<SelectPlayerTableModel, Boolean> fPlayerSelectionColumn;

	
	public SelectPlayersViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		initTable();
		
	}

	
	private void initTable() {
		fPlayerSelectionColumn = new Column<SelectPlayerTableModel, Boolean>(new CheckboxCell()) {
			@Override
			public Boolean getValue(SelectPlayerTableModel object) {
				return object.isPlayerSelected();
			}
		};
		fPlayerSelectionTable.addColumn(fPlayerSelectionColumn, fWizStrings.playerSelectedColumn());
		
		TextColumn<SelectPlayerTableModel> playerNameColumn = new TextColumn<SelectPlayerTableModel>() {

			@Override
			public String getValue(SelectPlayerTableModel object) {
				return object.getPlayerName();
			}
		};
		fPlayerSelectionTable.addColumn(playerNameColumn, fWizStrings.nameColumn());

	}

	
	@Override
	public void addPlayerSelectionFieldUpdater(FieldUpdater<SelectPlayerTableModel, Boolean> updater){
		fPlayerSelectionColumn.setFieldUpdater(updater);
	}
	
	@Override
	public void setTableData(ListDataProvider<SelectPlayerTableModel> data){
		data.addDataDisplay(fPlayerSelectionTable);
		fPlayerSelectionTable.setRowCount(data.getList().size());
		fPlayerSelectionTable.setVisibleRange(0, data.getList().size());
	}
	
	
	@Override
	public HasClickHandlers getDoneButtonClickHandlers() {
		return fDoneButton;
	}


	@Override
	public HasClickHandlers getAddPlayerButton() {
		return fAddPlayerButton;
	}


	@Override
	public HasText getAddPlayerNameText() {
		return fPlayerNameText;
	}
	
	@Override
	public void setTableSelectionModel(SelectionModel<SelectPlayerTableModel> selectionModel){
		fPlayerSelectionTable.setSelectionModel(selectionModel);
	}


	@Override
	public void refreshTable() {
		fPlayerSelectionTable.redraw();
	}
	
	@UiFactory
	CellTable<SelectPlayerTableModel> buildCellTable() {
		 CellTable.Resources resources = GWT.create(TableResources.class);
		return new CellTable<SelectPlayerTableModel>(100, resources);
	}


}
